package com.vnaloha.common.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vnaloha.common.dao.CustomerDAO;
import com.vnaloha.common.dao.OrdersDAO;
import com.vnaloha.common.dao.OrdersDetailDAO;
import com.vnaloha.common.dao.ProductDAO;
import com.vnaloha.common.model.CartItem;
import com.vnaloha.common.model.Orders;
import com.vnaloha.common.model.OrdersDetail;

@Service
public class OrdersServiceImpl implements OrdersService {

	@Autowired
	private OrdersDAO ordersDAO;

	@Autowired
	private OrdersDetailDAO ordersDetailDAO;

	@Autowired
	private CustomerDAO customerDAO;

	@Autowired
	private ProductDAO productDAO;

	@Override
	public Orders get(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@Transactional
	public boolean save(List<CartItem> cartItems, int customerId, String note,
			Float total) {

		try {
			Orders order = new Orders();
			order.setCustomer(customerDAO.get(customerId));
			Date date = new Date();
			order.setDate(date);
			order.setNote(note);
			order.setStatus(false);
			order.setValue(total);
			List<OrdersDetail> listOrdersDetail = new ArrayList<OrdersDetail>();
			for (CartItem cartTemp : cartItems) {
				listOrdersDetail.add(new OrdersDetail(productDAO.get(cartTemp
						.getId()), cartTemp.getQuatity()));
			}

			order.setOrderDetails(listOrdersDetail);
			return ordersDAO.save(order);
		} catch (Exception ex) {
			return false;
		}

	}

	@Override
	public List<Orders> list() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Orders> list(int start, int max) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		return false;
	}
}
